////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   ITEM_LINE
// AUTHOR           :   Alex Cheung
// DATE CREATE      :   2006/01/19
// DESCRIPTION      :   a ACTION file for the function, ITEM_LINE
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/01/19   Alex Cheung     1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.catalog.itemLine.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.catalog.itemLine.criteria.ItemLineSrchCriteria;
import com.erp.app.catalog.itemLine.pk.ItemLinePk;
import com.erp.app.catalog.itemLine.vo.ItemLineVo;
import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.app.setup.itemDetailTemplate.criteria.CatalogDtlTemplateSrchCriteria;
import com.erp.app.util.web.ActionErrorUtil;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.controller.action.SimplePageEditStyleAction;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;

/**
* A <strong>Page Edit Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request.  The controller (ActionServlet) will select an
* appropriate Action for each request.
* The main purpose for this action class is to handle the one-page document events
* from the web browsers.
*
* @author          Alex Cheung
* @version 1.0     2006/01/19
* @since           FrameWork 1.0
*
*/
public class ItemLineAction extends SimplePageEditStyleAction {

	////////////////////////////////////////////////////////////////////////////////
	// CONSTANTS DEFINITION
	////////////////////////////////////////////////////////////////////////////////

	//public static final String ACTION_DO_SOMETHING = "actionDoSomething";
	//public static final String STATUS_DO_SOMETHING = "statusDoSomething";

	public static final String ACTION_EDIT_ITEM_LINE = "editItemLine";
	public static final String LABEL_TO_BE_GEN = "<To be generated.>";
	public static final String ACTION_EDIT_ITEM_DETAIL_TEMPLATE = "editItemDetailTemplate";


	////////////////////////////////////////////////////////////////////////////////
	// GLOBAL VARIABLES DEFINITION
	////////////////////////////////////////////////////////////////////////////////



	////////////////////////////////////////////////////////////////////////////////
	// LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////

	public ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			if (!isHandled(request) && action.equalsIgnoreCase(ACTION_EDIT_ITEM_LINE)) {
				actionForward = processActionEditItemLine(mapping, formIn, request, response, errors);
				setHandled(request);
				saveToken(request);
			}else if (!isHandled(request) && action.equalsIgnoreCase(ACTION_EDIT_ITEM_DETAIL_TEMPLATE)) {
				actionForward = processActionEditItemDetailTemplate(mapping, formIn, request, response, errors);
				setHandled(request);
				saveToken(request);
			}

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}

		return actionForward;
	}

	protected ActionForward processOtherStatus(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, String rb_status) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
//			if (rb_status.equalsIgnoreCase(STATUS_DO_SOMETHING)) {
//				return processStatusDoSomething(mapping,  formIn,  request, response,  errors,  rb_status);
//			}
		} catch (Exception e) {
			Logger.log(this, e);
		}

		return actionForward;
	}

	//uncomment the following code and implement the method if necessary
	protected ActionForward processActionEditItemLine(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        ItemLineForm form = (ItemLineForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        String action = CommonUtil.nullToString(form.getAction());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        String editItemLine = form.getEditItemLine();
        if (!CommonUtil.isEmpty(editItemLine)) {
	        String companyCode = WebUtil.getAppropriateCompanyCode(session, null);

	        ItemLinePk pk = new ItemLinePk(companyCode, editItemLine);

	        session.removeAttribute(ProjectConstants.SESSION_EDIT_ITEM_LINE_PK);
	        session.setAttribute(ProjectConstants.SESSION_EDIT_ITEM_LINE_PK, pk);
	        actionForward = mapping.findForward(ACTION_EDIT_ITEM_LINE);

        }
        return actionForward;
    }

	protected ActionForward processActionEditItemDetailTemplate(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        ItemLineForm form = (ItemLineForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        String action = CommonUtil.nullToString(form.getAction());
        int pageEvenSrc = Integer.parseInt(form.getPageEventSrc());
        ArrayList rows = form.getMultiRows();
        ItemLineRow row = (ItemLineRow) rows.get(pageEvenSrc);

        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        CatalogDtlTemplateSrchCriteria criteria = new CatalogDtlTemplateSrchCriteria();
    	String companyCode = WebUtil.getAppropriateCompanyCode(session, null);
    	criteria.setSrchCompanyCode(companyCode);
    	criteria.setSrchNodeIdFr(row.getItemLine());
        session.removeAttribute(ProjectConstants.SESSION_EDIT_ITEM_DETAIL_TEMPLATE_CRITERIA);
        session.setAttribute(ProjectConstants.SESSION_EDIT_ITEM_DETAIL_TEMPLATE_CRITERIA, criteria);
        actionForward = mapping.findForward(ACTION_EDIT_ITEM_DETAIL_TEMPLATE);
        return actionForward;
    }



	////////////////////////////////////////////////////////////////////////////////
	// END OF LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// OVERRIDES PARENT'S OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////

	//the following method is only to be overwritten by sub-class if intended
	//to set the aut-search on-load to on/off
	protected boolean getLoadOnEnter() {

		//auto-search when load
		return true;
	}

	//the following method is only to be overwritten by sub-class if intended
	//to set the clearing search result sets on-load to on/off
	protected boolean getResetOnEnter() {

		//reset last searched results when load
		return true;
	}

	protected void initAction(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initAction(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
//			HashMap errMsgMap = WebUtil.getModuleErrorMessages(editingUser.getCompanyCode(),
//				DBMENU_GENERAL_MENU, form.getModuleName(), form.getSysLangCode(), DBMENU_OBJECT_TYPE_ERR_MSG);
//			form.setErrMsgMap(errMsgMap);

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void initForm(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initForm(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void initPage(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initPage(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeSearch(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterSearch(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeSave(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);


	}

	protected void afterSave(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeRefresh(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterRefresh(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeAddRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}


		//TODO: Add the following line to ProjectSecurityConstants.java
		//public static final String SP_SHARE_ITEM_LINE = "SP_SHARE_ITEM_LINE";
	protected void afterAddRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			ArrayList rows = form.getMultiRows();
			if (rows != null && rows.size() > 0) {
				ItemLineRow row = (ItemLineRow) rows.get(rows.size() - 1);
				row.setCompanyCode(WebUtil.getAppropriateCompanyCode(session, null));
				row.setDcInd(GENERIC_NO);
				row.setItemLine(String.valueOf(-1*(rows.size() - 1 )));
//				row.setItemLineVoAction(ACTION_ADD_DTL);
			}
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeDeleteRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterDeleteRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemLineForm form = (ItemLineForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
		ArrayList rows = form.getMultiRows();
		ItemLineRow row = (ItemLineRow) rows.get(Integer.parseInt(form.getPageEventSrc()));
		try {
			if(!ROW_MODE_CREATE.equals(row.getRowMode())){
				SelectDelegate dg = new SelectDelegate();
				String tmp = (String) dg.selectOneValue("select to_char(count(1)) from t_fabric_hdr a where a.node_ID =  " + row.getItemLine());
				if(!"0".equals(tmp)){
					 ActionErrorUtil.addErrorForSpecficPurpose(errors, " Unable to detele Item Group ID = " + row.getItemLine() + ", cause there are items mapped to this Category.");
				}
				if(errors.size() > 0){
					row.setIsDelete("");
				}
			}
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected String getSimpleSrchMenuId() {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_ITEM_LINE = "MOD_ITEM_LINE";
		return ProjectConstants.MOD_ITEM_LINE;
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// IMPLEMENTS PARENT'S ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected String getModuleName(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request,HttpServletResponse response) {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_ITEM_LINE = "MOD_ITEM_LINE";
		return ProjectConstants.MOD_ITEM_LINE;
	}

	protected String getFunctionId() {

		//struts-config xxxxx.do
		return "itemLine.do";
	}

	protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
		 HttpServletResponse response, ActionErrors errors) {

		ItemLineForm form = (ItemLineForm) formIn;
		form.setFormType(ProjectConstants.FORM_TYPE_PAGE_EDIT);
	}

	protected String getMasterDelegateName() {

		//the corresponding delegate session name
		return "itemLineDelegate";
	}

	protected Class getMasterFormClass() {

		//the corresponding form class
		return ItemLineForm.class;
	}

	protected Class getMasterVOClass() {

		//corresponding master vo class
		return ItemLineVo.class;
	}

	protected Class getDetailVOClass() {

		//corresponding detail vo class
		return ItemLineVo.class;
	}

	protected Class getDetailFormClass() {
		//corresponding detail row class
		return ItemLineRow.class;
	}

	protected String getDetailFormSessionName() {

		//corresponding detail form session name
		return "itemLineForm";
	}

	protected Class getSearchCriteriaClass() {

		//corresponding searching criteria class
		return ItemLineSrchCriteria.class;
	}

	protected void setOtherCriteria(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, Object criteria) {

		HttpSession session = request.getSession(false);
		User user = (User) session.getAttribute(ProjectConstants.SESSION_USER);
		ItemLineSrchCriteria crit = (ItemLineSrchCriteria) criteria;

		//TODO: Add the following line to ProjectSecurityConstants.java
		//public static final String SP_SHARE_ITEM_LINE = "SP_SHARE_ITEM_LINE";
		String companyCode = WebUtil.getAppropriateCompanyCode(session, null);

		crit.setSrchCompanyCode(companyCode);
		crit.setSearchResult(true);
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////
}
